#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define FILE_NAME "log.txt"

int main()
{

    //fprintf(stdout, "hello stdin\n");
    //fprintf(stderr, "hello Stderr\n");

    int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    dup2(fd, 1);
    printf("this is log.txt test\n");
    //char buffer[1024];
    //ssize_t s = read(0, buffer, 1024);
    //if(s > 0)
    //{
    //    buffer[s] = 0;
    //    //printf("%s\n", buffer);
    //    write(1, buffer, strlen(buffer));
    //}

    //close(1);
    //int fd = open(FILE_NAME, O_CREAT | O_WRONLY | O_TRUNC,0666);
    ////printf("xxxxxxxxxx\n");
    //fprintf(stdout, "aaaaaaaaa\n");

    
    //int fd = open(FILE_NAME, O_CREAT | O_WRONLY | O_APPEND, 0666);

    //dup2(fd, 1);
    //printf("hello open file\n");
    //fprintf(stdout, "stdout->fd:%d\n", fd);
    //close(fd);
    //close(1);
    //int fd = open(FILE_NAME, O_CREAT | O_WRONLY | O_APPEND, 0666);
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}

    //close(0);
    //int fd = open(FILE_NAME, O_RDONLY);
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}
    //char buffer[1024];
    //size_t size = fread(buffer, 1, sizeof buffer, stdin);
    //buffer[size] = '\0';
    //printf("%s\n", buffer);

   //printf("fd : %d\n", fd);
   //printf("-------------\n");

    //umask(0000);
    //int fd1 = open("log1.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd2 = open("log2.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd3 = open("log3.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd4 = open("log4.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);


    //printf("fd1:%d\n", fd1);
    //printf("fd2:%d\n", fd2);
    //printf("fd3:%d\n", fd3);
    //printf("fd4:%d\n", fd4);

    //close(fd1);
    //close(fd2);
    //close(fd3);
    //close(fd4);

    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}



   // FILE* fp = fopen("test.log", "a");
   // if(NULL == fp)
   // {
   //     perror("fopen");
   //     return 1;
   // }
   // 
   // const char* mes = "aaaaaaaaaaaaa\n";
   // fputs(mes, fp);

   //int cnt = 10;
   //while(cnt--)
   //{
   //    fputs("bbbb\n", fp);
   //}
   //fclose(fp);
   

    //close(1); //将标准输出文件显示器关闭
    //int fd = open("log.txt", O_CREAT | O_WRONLY | O_APPEND, 0666);//新创建文件，fd分配为1    
    //if(fd < 0)    
    //{    
    //    perror("open");    
    //    return 1;                                                         
    //}    
    //printf("what will happen\n");

    //close(0);
    //int fd = open("log.txt", O_RDONLY);
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}

    //char buffer[1024];
    //fread(buffer, 1, 1024, stdin);
    //printf("stdin->fd:%d, %s\n", stdin->_fileno, buffer);
    return 0;
}
